package gui;

import gui.author.AuthorCenterPanel;
import gui.author.AuthorNorthPanel;
import gui.author.AuthorSouthPanel;
import gui.programchair.pcCenterPanel;
import gui.programchair.pcNorthPanel;
import gui.programchair.pcSouthPanel;
import gui.reviewer.ReviewerCenterPanel;
import gui.reviewer.ReviewerNorthPanel;
import gui.reviewer.ReviewerSouthPanel;
import gui.welcome.WelcomeMainPanel;

import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import caobject.CALoader;
import caobject.Conference;
import caobject.EnumRole;
import caobject.User;

/**
 * The main conference assistant window frame
 * 
 * @author Bob Rankin
 * @author Brian M. Luger
 * @author Tom Netzer(added south panel to program chair frame)
 * @version 11/27/2011
 */
@SuppressWarnings("serial")
public class CAFrame extends JFrame
{
	/**
	 * Default Conference Data Save/Load Location
	 */
	private static final String DATA_LOC = "ConfData.txt";
	
	/**
	 * Frame Width
	 */
	private static final int WIDTH = 800;
	
	/**
	 * Frame Height
	 */
	private static final int HEIGHT = 600;
	
	/**
	 * CA Control
	 */
	private final CAControl my_control;
	
	/**
	 * The Conferences
	 */
	private final ArrayList<Conference> my_confList;
	
	public CAFrame() {
		my_confList = CALoader.load(DATA_LOC);
		my_control = new CAControl();
		
		init();
	}
	
	private void init() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(new BorderLayout());
		setSize(WIDTH, HEIGHT);
		setResizable(false);
		
		// Save Conference Data Before Closing ...
		addWindowListener(new WindowAdapter() {
			public void windowClosing(final WindowEvent event) {
				
				String[] opts = {"Yes", "No"};
				
				int choice = JOptionPane.showOptionDialog(CAFrame.this, 
						"Would you like to save your Conference Data?", 
						"Save",
						JOptionPane.YES_NO_OPTION,
						JOptionPane.QUESTION_MESSAGE, 
						null, 
						opts, 
						"Yes");
				
				if (choice == JOptionPane.YES_OPTION) {
					CALoader.save(DATA_LOC, my_confList);
					
					JOptionPane.showMessageDialog(CAFrame.this, 
							"Conference Data has been saved!", 
							"Save", 
							JOptionPane.INFORMATION_MESSAGE);
				} else {
					JOptionPane.showMessageDialog(CAFrame.this, 
							"Conference Data was NOT saved!", 
							"Not Saved", 
							JOptionPane.INFORMATION_MESSAGE);
				}
			}
		});
		
		displayWelcome();
		setVisible(true);
	}
	
	/**
	 * Determines which panel to display
	 * 
	 * @param the_role Role used to determine panel
	 */
	public void display(final Conference the_conf, 
			final User the_user, final EnumRole the_role) {
		switch (the_role) {
			case AUTHOR:
				getContentPane().removeAll();
				add(new AuthorNorthPanel(the_conf, 
						my_control, the_user), BorderLayout.NORTH);
				add(new AuthorCenterPanel(the_conf, my_control), 
						BorderLayout.CENTER);
				add(new AuthorSouthPanel(CAFrame.this, the_conf, 
						my_control), BorderLayout.SOUTH);
				setVisible(true);
				break;
				
			case REVIEWER:
				getContentPane().removeAll();
				add(new ReviewerNorthPanel(the_conf, 
						my_control, the_user), BorderLayout.NORTH);
				add(new ReviewerCenterPanel(the_conf, the_user, my_control),
						BorderLayout.CENTER);
				add(new ReviewerSouthPanel(CAFrame.this, the_conf, 
						my_control), BorderLayout.SOUTH);
				setVisible(true);
				break;
				
			case PROGRAMCHAIR:
				getContentPane().removeAll();
				add(new pcNorthPanel(the_conf, my_control, the_user), 
						BorderLayout.NORTH);
				add(new pcCenterPanel(CAFrame.this, the_conf), 
						BorderLayout.CENTER);
				add(new pcSouthPanel(CAFrame.this),
						BorderLayout.SOUTH);
				setVisible(true);
				break;
				
			default:
				break;
		}
	}
	
	/**
	 * Displays the Welcome Screen
	 */
	public void displayWelcome() {
		getContentPane().removeAll();
		add(new WelcomeMainPanel(CAFrame.this, my_confList), 
				BorderLayout.CENTER);
		setVisible(true);
	}
}

